package com.xatime.auth.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by mason on 18-12-6.
 */
@Service
@Slf4j
public class CachingClientDetailsService implements ClientDetailsService {

    private static final Map<String, ClientDetails> clients = new HashMap<>();

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        ClientDetails clientDetails = clients.get(clientId);
        if (clientDetails != null) {
            log.debug("直接返回缓存：{}",clientId);
            return clientDetails;
        }
        BaseClientDetails details = new BaseClientDetails(clientId, "system", "server", "password", "");
        details.setClientSecret("$2a$10$9xma5IWV7g89vPPcGNj88OH53ls5j5stbawve84WzfSpfk5ynVUtq");
        clients.put(clientId, details);
        return details;
    }
}
